package adapters;

import android.content.Context;
import android.content.DialogInterface;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.spzhao.qrcodescaner.R;

import org.json.JSONException;

import java.util.ArrayList;

import beans.QrMessage;
import config.AppConfig;
import utils.QrColor;

/**
 * Created by zhaoshengping on 2017/2/24.
 */

public class QrCodeMessageAdapter extends BaseAdapter {

    private ArrayList<QrMessage> qrMessageArrayList;
    private Context mContext;
    private  String[] color_arr;

    public QrCodeMessageAdapter (Context context, ArrayList<QrMessage> qrMessages) {
        this.mContext = context;
        this.qrMessageArrayList = qrMessages;
        this.color_arr = AppConfig.COLOR_STRING_ARR;
    }
    @Override
    public int getCount() {
        return qrMessageArrayList.size();
    }

    @Override
    public QrMessage getItem(int i) {
        return qrMessageArrayList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    public void removeItem(int i) {
        qrMessageArrayList.remove(i);
        notifyDataSetChanged();
    }

    public void addItem(QrMessage item) {
        qrMessageArrayList.add(item);
        notifyDataSetChanged();
    }
    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        final ViewHolder holder;
        final QrMessage qrMessage = getItem(i);
        if (view == null) {
            holder = new ViewHolder();
            view = LayoutInflater.from(mContext).inflate(R.layout.qr_item,null);
            holder.title = (TextView) view.findViewById(R.id.qrTitle);
            holder.time = (TextView) view.findViewById(R.id.qrTime);
            holder.image = (ImageView) view.findViewById(R.id.qrImage);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }

        int curColor = 0;

        Log.i("color: ",qrMessage.getColor());
        switch (qrMessage.getColor()) {
            case "#000":
                holder.image.setImageResource(R.drawable.circle_color_black);
                curColor = 0;
                break;
            default:
                holder.image.setImageResource(R.drawable.ring_color_green);
                curColor = 1;
                break;
        }

        final int finalCurColor = curColor;
        holder.image.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                final QrColor qrColor = new QrColor(mContext) {
                    @Override
                    public void itemClick(DialogInterface dialogInterface, int i) {
                        Log.i("color ", Integer.toString(i));
                        qrMessage.setColor(color_arr[i]);
                        notifyDataSetChanged();
                        try {
                            qrMessage.saveOrUpdate();
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        switch (i) {
                            case 0:
                                holder.image.setImageResource(R.drawable.circle_color_black);
                                break;
                            case 1:
                                holder.image.setImageResource(R.drawable.ring_color_green);
                                break;
                            default:
                                holder.image.setImageResource(R.drawable.ring_color_green);
                                break;
                        }
                    }
                };
                qrColor.AlertColorChoose(finalCurColor);
            }
        });
        holder.title.setText(qrMessage.getName());
        holder.time.setText(qrMessage.getCreateTime());


        return view;
    }

    private class ViewHolder {
        TextView title;
        TextView time;
        ImageView image;
    }

    public void setData (ArrayList<QrMessage> datas) {
        this.qrMessageArrayList.clear();
        this.qrMessageArrayList = datas;
        notifyDataSetChanged();
    }
}
